export default {
  // 开启命名空间
  namespaced: true,

  // 数据
  state: () => ({
    address: (() => {
      try {
        const data = uni.getStorageSync('address');
        return data ? JSON.parse(data) : {};
      } catch (e) {
        console.error('解析 address 缓存失败', e);
        return {};
      }
    })(),
   token: (() => {
         try {
           return uni.getStorageSync('token') || '';
         } catch (e) {
           console.error('获取 token 失败', e);
           return '';
         }
       })(),
    // 用户的信息对象
    userinfo: (() => {
      try {
        const data = uni.getStorageSync('userinfo');
        return data ? JSON.parse(data) : {};
      } catch (e) {
        console.error('解析 userinfo 缓存失败', e);
        return {};
      }
    })(),
	 // 重定向的 object 对象 { openType, from }
	redirectInfo: null
  }),

  getters: {
    addstr(state) {
      if (!state.address.provinceName) return '';
      return (
        state.address.provinceName +
        state.address.cityName +
        state.address.countyName +
        state.address.detailInfo
      );
    },
  },

  // 方法
  mutations: {
    // 更新收货地址
    updateAddress(state, address) {
      state.address = address;
      this.commit('m_user/saveAddressToStorage');
    },

    // 持久化存储 address
    saveAddressToStorage(state) {
      uni.setStorageSync('address', JSON.stringify(state.address));
    },

    updateUserInfo(state, userinfo) {
      state.userinfo = userinfo;
      this.commit('m_user/saveUserInfoToStorage');
    },

    // 持久化存储 userinfo
    saveUserInfoToStorage(state) {
      uni.setStorageSync('userinfo', JSON.stringify(state.userinfo));
    },
	updateToken(state,token){
		state.token = token
		this.commit('m_user/saveTokenToStorage')
	},
	saveTokenToStorage(state){
		uni.setStorageSync('token',state.token)
	},
	updateRedirectInfo(state,info){
		state.redirectInfo = info
		console.log(state.redirectInfo)
		
	}
	
  },

  actions: {},
};